/*
 * Copyright (c) 2010-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.api;

/**
 * A context is the mechanism by which host application values are made available
 * to a running script. Contexts form a graph of immutable objects, where a
 * value that cannot be found in one context delegates to a parent context until
 * the value is found. For example, imagine a multi-tenant host application that
 * has a set of values available to all tenants, then a set of values accessible
 * only to that tenant, and finally a set of values accessible from a particular
 * point (thus, the word <i>context</i>) of the application:
 * <pre>
 *     everybody
 *     		tenant-A
 *     			shopping-cart
 *     			inventory
 *     			user-profile
 *     		tenant-B
 *     			shopping-cart
 *     			inventory
 *     			user-profile
 *     		...
 * </pre>
 * In this example, a new context specific to the shopping cart portion of the
 * application is created, with the common tenant context as a parent, which in
 * turn has "everybody" as the root context. By reusing these objects, which are
 * inherently thread safe and re-entrant, context setup costs are greatly
 * reduced and allow for safe parallel execution as well. See the Axil main
 * API class for details on creating contexts.
 *
 * <h4>Dynamic Contexts</h4>
 * The Axil runtime provides a general purpose implementation of Context suitable
 * for most host applications. However, a custom context may be desirable if
 * the host application needs to make a large number of objects available for
 * script execution. Since constructing the map of name/value pairs could be
 * far costlier than execution of the script, a custom context can be used to
 * perform the lookup operation dynamically instead of creating a map.
 * <p/>
 * This interface may be implemented by host applications, but that is rarely
 * necessary. For most applications, use the Axil.instance() object to create
 * instances of this object.
 */
public interface Context {
	/**
	 * Extract the current value for the given identifier from this context.
	 * This method is used only to extract root objects, not complex identifiers.
	 * For example, in the complex identifier `Shopping Cart.Items`, the get()
	 * method is used only to extract 'shopping-cart', not the entire value.
	 * This method is invoked by Accessor objects only.
	 *
	 * @param name
	 * 	The name of the identifier, either from a simple identifier or the root
	 * 	element from a compound identifier.
	 *
	 * @return
	 * 	Returns an appropriate object value. The null value may be safely
	 * 	returned. If this context does not contain the value, then it must
	 * 	delegate to the parent context. If the object is simply not in the
	 * 	context, then null is returned.
	 */
	public Object get(String name);


	/**
	 * Extract the current value for the given identifier from this context.
	 *
	 * @param name
	 *	The name of the identifier, either from a simple identifier or the root
	 * 	element from a compound identifier.
	 *
	 * @return
	 * 	Returns the Axil object matching the given key. If this context does
	 * 	not contain the value, then it must delegate to the parent context. If
	 * 	the object is simply not in the context, then null is returned.
	 */
	public AxilObject at(String name);


	/**
	 * Return the parent context on top of which this one relies. The contexts
	 * form a chain of immutable objects, allowing for the same contexts to be
	 * used safely across threads and also allow common identifiers to be defined
	 * only once.
	 *
	 * @return
	 * 	Returns the parent context. For a custom context implementation by the
	 * 	host application, the object returned can never be null.
	 */
	public Context parent();
}
